package com.server.http.handler.impl;

import java.util.Map;

import com.server.conditionenum.HttpReturnStatus;
import com.server.http.handler.HttpHandler;
import com.server.http.handler.domain.JsonUtil;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.AppData;
import com.server.tools.HttpUtils;

public class GetOpenGID extends HttpHandler{
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		String failMsg = null;
		String sessionKey = request.getParameter("sessionKey");
		String encrypted = request.getParameter("encrypted");
		String iv = request.getParameter("iv");
		String returnData = "";
		
		long appid = 0;
		try {
			if(sessionKey != null && sessionKey != "" && encrypted != null && encrypted != "" && iv != null && iv != "") {
				sessionKey = java.net.URLEncoder.encode(sessionKey,"UTF-8");
				encrypted = java.net.URLEncoder.encode(encrypted,"UTF-8");
				iv = java.net.URLEncoder.encode(iv,"UTF-8");
				appid = Long.parseLong(request.getParameter("appid"));
				if(AppData.appMap.containsKey(appid)) {
					String url = "http://127.0.0.1/userver/wxdec/aes.php?wx_appid=" + AppData.appMap.get(appid).wx_appid + "&sessionKey=" + sessionKey + "&encrypted=" + encrypted + "&iv=" + iv;
					returnData = HttpUtils.GetHttpData(url,"","GET");
					Map<String, String> data = JsonUtil.JsonToObjectMap(returnData);
					if(data.containsKey("openGId")) {
						returnData = data.get("openGId");
					}else {
						failMsg = "解析错误!";
					}
				}else {
					failMsg = "appid不存在!";
				}
			}else {
				failMsg = "上传的参数错误!";
			}
		}catch (Exception e) {
			e.printStackTrace();
			failMsg = "上传的参数错误!";
		}
		
		if(failMsg == null) {
			SetReturnData(response,returnData,HttpProtocol.GETOPENGID,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.GETOPENGID,HttpReturnStatus.Failed);
		}
	}
}